import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/utils/permission_helper.dart';
// import 'package:flutter_syt/utils/permission_util.dart';
// import 'package:qr_code_scanner/qr_code_scanner.dart';

class QRCodePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _QRCodePageState();
  }
}

class _QRCodePageState extends State<QRCodePage> with TickerProviderStateMixin {
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  bool _canScan = true;
  // QRViewController? _qrViewController;
  late AnimationController _controller;
  late Animation<Offset> _animation;

  Future<bool>? _future;
  @override
  void dispose() {
    // _qrViewController?.dispose();
    _controller.dispose();
    super.dispose();
  }

  @override
  void reassemble() {
    super.reassemble();
    // if (Platform.isAndroid) {
    //   _qrViewController?.pauseCamera();
    // } else if (Platform.isIOS) {
    //   _qrViewController?.resumeCamera();
    // }
  }

  @override
  Widget build(BuildContext context) {
    // var scanArea = (MediaQuery.of(context).size.width < 400 ||
    //         MediaQuery.of(context).size.height < 400)
    //     ? 150.0
    //     : 300.0;
    return Scaffold(
      appBar: AppBar(
        title: Text(S.of(context).scan),
      ),
      body: Container(
          color: Colors.black,
          child: Stack(
            children: [
              FutureBuilder<bool>(
                future: _future,
                builder: (context, snapshot) {
                  // DLog("snapshot  ${snapshot.hasData}");
                  // if (snapshot.hasData && snapshot.data == true) {
                  //   return QRView(
                  //     key: qrKey,
                  //     onQRViewCreated: (controller) {
                  //       _qrViewController = controller;
                  //       controller.resumeCamera();
                  //       var onSub = (Barcode event) async {
                  //         if (!_canScan) {
                  //           return;
                  //         }
                  //         _canScan = false;
                  //         var content = event.code ?? "";
                  //         Navigator.of(context).pop(content);
                  //       };
                  //       controller.scannedDataStream.listen(onSub);
                  //     },
                  //   );
                  // }
                  return Container();
                },
              ),
              Positioned.fill(
                  child: SlideTransition(
                      position: _animation,
                      //将要执行动画的子view
                      child: Column(
                        children: [
                          Container(
                              alignment: Alignment.topCenter,
                              height: 92.w,
                              width: 1.sw,
                              // color: Colors.red,
                              child: Image.asset(
                                "assets/icons/icon_new_qr.png",
                              )),
                        ],
                      ))),
              Positioned(
                  bottom: 60.w,
                  left: 0,
                  right: 0,
                  child: Text(
                    S().scan_location_qr_code1,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.w500),
                  ))
            ],
          )),
    );
  }

  @override
  void initState() {
    super.initState();
    // Future.delayed(Duration.zero, () {
    _future = PermissionHelper.checkCameraPermission(appContext.context!);
    // });

    _controller = AnimationController(
        duration: Duration(milliseconds: 2000), vsync: this);
    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.value = 0;
        _controller.forward();
        // _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        // _controller.forward();
      }
    });
    _animation =
        Tween(begin: Offset(0, 0.1), end: Offset(0.0, .7)).animate(_controller);
    //开始执行动画
    _controller.forward();
  }
}
